using System.Collections;
using Concurso;
using Curriculum.Services;
using Utiles;

namespace ConcursoCV.UI.WebUserControls.Inscripcion
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for ListadoConcursosInscripcion.
	/// </summary>
	public class ListadoConcursosInscripcion : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.ValidationSummary valSum;
		protected Pluspetrol.Core.UIControls.WebControls.CDatePicker dtpFecha;
		protected System.Web.UI.WebControls.RegularExpressionValidator revActDate;
		protected System.Web.UI.WebControls.TextBox txtNombre;
		protected System.Web.UI.WebControls.DropDownList ddlDepartamento;
		protected System.Web.UI.WebControls.DropDownList ddlArea;
		protected System.Web.UI.WebControls.Button btnBuscar;
		protected System.Web.UI.WebControls.Button btnLimpiar;
		protected System.Web.UI.WebControls.DataGrid dtgConcursos;
		protected System.Web.UI.WebControls.Label lblCount;

		private const string pidConcursoInscripcion = "014";
		
		private const string INSCRIPTO = "INSCRIPTO";

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{
				this.DocenteId  = Helper.SecurityHelper.GetUsuarioID();
				this.Tipo = this.Request["type"];
				InitializeVariables();
				PopulateCombos();
			}
		}

		private void PopulateCombos()
		{			
			PopulateComboDepartamento();
			PopulateComboArea();
		}
		private void PopulateComboDepartamento()
		{
			Helper.ListHelper.FillDropDownListLookup(ddlDepartamento,"DEPART",true);
		}
		private void PopulateComboArea()
		{
			Helper.ListHelper.FillDropDownListLookup(ddlArea,"AREA",true);
		}

		private void InitializeVariables()
		{
			SortColumn = "Nombre";
			SortDirection = true;			
		}

		private void FillSearchVariables() 
		{
			FiltroNombre = txtNombre.Text.Trim() != string.Empty ? txtNombre.Text.Trim() : string.Empty;
			FiltroFecha = null;
			FiltroDepartamentoId = ddlDepartamento.SelectedIndex > 0 ? ddlDepartamento.SelectedValue  : string.Empty;
			FiltroAreaId = ddlArea.SelectedIndex > 0 ? ddlArea.SelectedValue  : string.Empty;

			if( dtpFecha.Text != string.Empty )
			{
				FiltroFecha = DateTime.Parse( dtpFecha.Text );
			}			
		}

		private IList GetListBySearch() 
		{
			// Search						
			if(	this.Tipo == INSCRIPTO	)
			{
				ConcursoService service = new ConcursoService();
				return service.ObtenerConcursosInscripto( DocenteId );
			}
			else
			{
				DocenteService service = new DocenteService();
				return service.ObtenerConcursosAbiertosNoInscripto(DocenteId);
			}				
		}


		private void FillDataGridWithSearch()
		{
			IList list = GetListBySearch();
			((ArrayList)list).Sort(new GenericComparer(SortColumn,SortDirection));
			dtgConcursos.DataSource = list;
			lblCount.Text = list.Count.ToString();
			try
			{
				dtgConcursos.DataBind();
			}
			catch(Exception )
			{
				dtgConcursos.CurrentPageIndex = 0;
				FillDataGridWithSearch();
			}

			list = null;
		}

		private void RedirectToConcursoABM(PageMode mode, string concursoId)
		{
			string url = "Default.aspx?pid=" + pidConcursoInscripcion + "&concursoId=" + concursoId + "&docenteId=" + DocenteId + "&PageMode=" + PageModeConverter.ToInt( mode ) + "&tipo=" + this.Tipo;
			Server.Transfer(url);
		}

		#region - properties -

		public string FiltroNombre
		{
			get{ return (string)ViewState["_filtroNombre"];}
			set{ ViewState["_filtroNombre"] = value;}
		}
		public object FiltroFecha
		{
			get{ return ViewState["_filtroFecha"];}
			set{ ViewState["_filtroFecha"] = value;}
		}
		public string FiltroDepartamentoId
		{
			get{ return (string)ViewState["_filtroDepartamentoId"];}
			set{ ViewState["_filtroDepartamentoId"] = value;}
		}
		public string FiltroAreaId
		{
			get{ return (string)ViewState["_filtroAreaId"];}
			set{ ViewState["_filtroAreaId"] = value;}
		}

		private string DocenteId
		{
			get{ return (string)ViewState["_docenteId"];}
			set{ ViewState["_docenteId"] = value;}			
		}
		private string Tipo
		{
			get{ return (string)ViewState["_tipo"];}
			set{ ViewState["_tipo"] = value;}			
		}

		#region - Sort Variables -

		public string SortColumn
		{
			get{ return (string)ViewState["SortColumn"];}
			set{ ViewState["SortColumn"] = value;}
		}
		public bool SortDirection
		{
			get{ return (bool)ViewState["SortDirection"];}
			set{ ViewState["SortDirection"] = value;}
		}

		#endregion - Sort Variables -

		#endregion - properties -

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnBuscar.Click += new System.EventHandler(this.btnBuscar_Click);
			this.dtgConcursos.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtgConcursos_ItemCommand);
			this.dtgConcursos.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dtgConcursos_PageIndexChanged);
			this.dtgConcursos.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dtgConcursos_SortCommand);
			this.dtgConcursos.SelectedIndexChanged += new System.EventHandler(this.dtgConcursos_SelectedIndexChanged);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void dtgConcursos_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if ( e.CommandName != "Page" )
			{
				string concursoId = e.Item.Cells[0].Text;
				switch ( e.CommandName )
				{
					case "Ver":
						RedirectToConcursoABM(PageMode.VIEW,concursoId);
						break;
					default:
						throw new Exception("comando desconocido");
				}
			}		
		}

		private void btnBuscar_Click(object sender, System.EventArgs e)
		{
			dtgConcursos.CurrentPageIndex=0;
			FillSearchVariables();
			FillDataGridWithSearch();		
		}

		private void dtgConcursos_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			//Si se hace click sobre ,la misma columna, cambia el orden
			if (SortColumn.ToString() == e.SortExpression.ToString())
				SortDirection = !SortDirection;
			
			SortColumn = e.SortExpression;
			FillDataGridWithSearch();						
		}
		
		private void dtgConcursos_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			dtgConcursos.CurrentPageIndex = e.NewPageIndex;
			FillDataGridWithSearch();		
		}

		private void dtgConcursos_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			dtpFecha.Text = string.Empty;
			txtNombre.Text = string.Empty;
			ddlArea.SelectedIndex = 0;
			ddlDepartamento.SelectedIndex = 0;
		}
	}
}
